package cn.iocoder.yudao.module.erp.controller.admin.gi.gigz;

import cn.iocoder.yudao.module.erp.controller.admin.gi.gigz.vo.GiGzPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gigz.vo.GiGzRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gigz.vo.GiGzSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.giyw.vo.GiYwSaveReqVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.module.erp.dal.dataobject.gi.GiGzDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.giitem.GiItemGzDO;
import cn.iocoder.yudao.module.erp.service.gi.GiGzService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - ERP物流广州仓库")
@RestController
@RequestMapping("/erp/gi-gz")
@Validated
public class GiGzController {

    @Resource
    private GiGzService giGzService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP物流广州仓库")
//    @PreAuthorize("@ss.hasPermission('erp:gi-gz:create')")
    public CommonResult<String> createGiGz(@Valid @RequestBody GiGzSaveReqVO createReqVO) {
        return success(giGzService.createGiGz(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP物流广州仓库")
//    @PreAuthorize("@ss.hasPermission('erp:gi-gz:update')")
    public CommonResult<Boolean> updateGiGz(@Valid @RequestBody GiGzSaveReqVO updateReqVO) {
        giGzService.updateGiGz(updateReqVO);
        return success(true);
    }

    @PutMapping("/examine")
    @Operation(summary = "ERP物流广州仓库单据审核")
//    @PreAuthorize("@ss.hasPermission('erp:gi-gz:examine')")
    public CommonResult<Boolean> examineGiGz(@Valid @RequestBody GiGzDO giGzDO) {
        giGzService.examineGiGz(giGzDO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP物流广州仓库")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:gi-gz:delete')")
    public CommonResult<Boolean> deleteGiGz(@RequestParam("id") String id) {
        giGzService.deleteGiGz(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP物流广州仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:gi-gz:query')")
    public CommonResult<GiGzRespVO> getGiGz(@RequestParam("id") String id) {
        GiGzDO giGz = giGzService.getGiGz(id);
        return success(BeanUtils.toBean(giGz, GiGzRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP物流广州仓库分页")
    // @PreAuthorize("@ss.hasPermission('erp:gi-gz:query')")
    public CommonResult<PageResult<GiGzRespVO>> getGiGzPage(@Valid GiGzPageReqVO pageReqVO) {
        pageReqVO.setPageSize(-1);
        PageResult<GiGzDO> pageResult = giGzService.getGiGzPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GiGzRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP物流广州仓库 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:gi-gz:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGiGzExcel(@Valid GiGzPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GiGzDO> list = giGzService.getGiGzPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP物流广州仓库.xls", "数据", GiGzRespVO.class,
                        BeanUtils.toBean(list, GiGzRespVO.class));
    }

    // ==================== 子表（ERP物流广州仓库货号详情） ====================

    @GetMapping("/gi-item-gz/list-by-no")
    @Operation(summary = "获得ERP物流广州仓库货号详情列表")
    @Parameter(name = "no", description = "入库编号")
    // @PreAuthorize("@ss.hasPermission('erp:gi-gz:detail')")
    public CommonResult<List<GiItemGzDO>> getGiItemGzListByNo(@RequestParam("no") String no) {
        return success(giGzService.getGiItemGzListByNo(no));
    }

}